<%@page import="java.util.Calendar"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.DateFormat"%>
<%@page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">

<script>
function daysBetween(){
	var date1=document.getElementById("date1").value;
	
	var date2=document.getElementById("date2").value;
	   if (date1.indexOf("-") != -1) { date1 = date1.split("-"); } else if (date1.indexOf("/") != -1) { date1 = date1.split("/"); } else { return 0; } 
	   if (date2.indexOf("-") != -1) { date2 = date2.split("-"); } else if (date2.indexOf("/") != -1) { date2 = date2.split("/"); } else { return 0; } 
	   if (parseInt(date1[0], 10) >= 1000) { 
	       var sDate = new Date(date1[0]+"/"+date1[1]+"/"+date1[2]); 
	   } else if (parseInt(date1[2], 10) >= 1000) { 
	       var sDate = new Date(date1[2]+"/"+date1[0]+"/"+date1[1]); 
	   } else { 
	       return 0; 
	   } 
	   if (parseInt(date2[0], 10) >= 1000) { 
	       var eDate = new Date(date2[0]+"/"+date2[1]+"/"+date2[2]); 
	   } else if (parseInt(date2[2], 10) >= 1000) { 
	       var eDate = new Date(date2[2]+"/"+date2[0]+"/"+date2[1]); 
	   } else { 
	       return 0; 
	   } 
	   var one_day = 1000*60*60*24; 
	   var daysApart = Math.abs(Math.ceil((sDate.getTime()-eDate.getTime())/one_day)); 
	   if(daysApart<7){
		   var fine="0";
		  document.form.result.value=fine;
	   }else{
		   daysApart=(daysApart-7)*3;
		  document.form.result.value=daysApart;
	   }  
	   return false;
	} 
function showData(id) {
	xmlHttp = GetXmlHttpObject()
	var url = "datefine.jsp";
	url = url + "?id=" + id;
	xmlHttp.onreadystatechange = stateChanged
	xmlHttp.open("GET", url, true)
	xmlHttp.send(null)
}
function stateChanged() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		var showdata = xmlHttp.responseText;
		var strar = showdata.split(":");
		if (strar.length > 1) {
			var strname = strar[1];
		
			document.getElementById("date1").value = strar[2];
		

		}
	}
}
function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
</script>
</head>
<body style="background-color: grey">
	<%
		String DATE_FORMAT = "yyyy-MM-dd";
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		Calendar c1 = Calendar.getInstance();
		String date2 = sdf.format(c1.getTime());
	%>
	<form name="form" onSubmit="return daysBetween();">
	<fieldset>
			<legend align="top">
				<h2 style="color: red">Fine Calculator</h2>
			</legend>
		<center>
			
			<table>
				<tr>
					<td align="left">Book Id</td>
					<td><input type="text" name="id" id="id" onkeyup="showData(this.value);" value="">
					</td>
				</tr>
				<tr>
					<td align="left">Book Issued Date</td>
					<td><input type="text" id="date1" name="date1" value="">
					</td>
				</tr>
				<tr>
					<td align="left">Today Date</td>
					<td><input type="text" id="date2" name="date2"
						value="<%=date2%>">
					</td>
				</tr>
				<tr>
					<td align="left">Fine</td>
					<td><input type="text" name="result" value=""></td>
				</tr>
				<tr>
					<td><input type="submit" value="submit">
					</td>
				</tr>
			</table>
		</center>
	</form>
	</fieldset>
